package com.ruoyi.flowable.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.flowable.domain.bo.AppBo;
import com.ruoyi.flowable.domain.report.Condition;
import com.ruoyi.flowable.domain.report.Report;
import com.ruoyi.flowable.domain.report.SearchForm;
import com.ruoyi.flowable.domain.report.TableColumn;
import com.ruoyi.flowable.domain.vo.*;
import com.ruoyi.flowable.service.*;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/front")
public class FrontController extends BaseController {

    @Autowired
    private ISysPostService postService;

    @Autowired
    private IAppService appService;
    @Autowired
    private IAppVersionService appVersionService;

//    @Autowired
//    private ISysUserService userService;

    @Autowired
    private IFormService formService;

    @Autowired
    private IFormVersionService formVersionService;

    @Autowired
    private IFormDataService formDataService;

    @Autowired
    private IBpmnConfigService bpmnConfigService;


    @GetMapping(value = "/listApp")
    public R<List<AppVo>> listApp() {
        //TODO 过滤条件
        AppBo app = new AppBo();
        app.setStatus("0");
        return R.ok(appService.queryList(app));
    }

    @GetMapping(value = "/getApp/{appId}")
    public R<AppVo> getApp(@PathVariable("appId") Long appId) {
        return R.ok(appService.queryById(appId));
    }

    @GetMapping(value = "/getForm/{formId}")
    public R<FormVo> getForm(@PathVariable("formId") Long formId) {
        return R.ok(formService.queryById(formId));
    }

    @GetMapping(value = "/getAppVersion/{appVersionId}")
    public R<AppVersionVo> getAppVersion(@PathVariable("appVersionId") Long appVersionId) {
        return R.ok(appVersionService.queryById(appVersionId));
    }

    @GetMapping(value = "/getFormVersion/{formVersionId}")
    public R<FormVersionVo> getFormVersion(@PathVariable("formVersionId") Long formVersionId) {
        return R.ok(formVersionService.queryById(formVersionId));
    }

    @GetMapping(value = "/getBpmnConfig/{appVersionId}")
    public R<BpmnConfigVo> getBpmnConfig(@PathVariable("appVersionId") Long appVersionId) {
        return R.ok(bpmnConfigService.selectBpmnConfigByAppVersionId(appVersionId));
    }

    @GetMapping(value = "/getFormDataByInstId/{procInstId}")
    public R<?> getFormDataByInstId(@PathVariable("procInstId") String procInstId) {
        return R.ok(formDataService.selectFormDataByInstId(procInstId));
    }

    @GetMapping(value = "/getDataByInstId/{procInstId}")
    public R<?> getDataByInstId(@PathVariable("procInstId") String procInstId) {
        FormDataVo formData = formDataService.selectFormDataByInstId(procInstId);
        if (formData == null) {
            return R.fail("发起数据不存在");
        }
        FormVersionVo formVersion = formVersionService.queryById(formData.getFormVersionId());
        AppVersionVo appVersion = appVersionService.queryById(formData.getAppVersionId());
        BpmnConfigVo bpmnConfig = bpmnConfigService.selectBpmnConfigByAppVersionId(formData.getAppVersionId());
        Map<String, Object> res = new HashMap<>();
        res.put("formData", formData);
        res.put("formVersion", formVersion);
        res.put("appVersion", appVersion);
        res.put("bpmnConfig", bpmnConfig);
        return R.ok(res);
    }

    @GetMapping("/listPost")
    public R<List<SysPost>> listPost() {
        return R.ok(postService.selectPostAll());
    }



    @GetMapping(value = "/getFormAndTable/{formId}")
    public R<?> getFormAndTable(@PathVariable("formId") Long formId) {
        FormVo form = formService.queryById(formId);
        if (null == form) {
            return R.fail("表单不存在");
        }
        List<Report> reports = JsonUtils.parseArray(form.getReportConfig(), Report.class);
        reports = reports.stream().sorted(Comparator.comparing(Report::getSort))
            .collect(Collectors.toList());
        List<SearchForm> searchForms = new ArrayList<>();
        List<TableColumn> tableColumns = new ArrayList<>();
        TableColumn tableColumn = new TableColumn();
        tableColumn.setType("index");
        tableColumn.setLabel("序号");
        tableColumns.add(tableColumn);
        for (Report report : reports) {
            if ("1".equals(report.getIsList())){
                TableColumn tableColumn1 = new TableColumn();
                tableColumn1.setDataName(report.getColumnName());
                tableColumn1.setLabel(report.getColumnComment());
                tableColumns.add(tableColumn1);
            }
            if ("1".equals(report.getIsQuery())){
                SearchForm searchForm = new SearchForm();
                searchForm.setLabel(report.getColumnComment());
                searchForm.setModelName(report.getColumnName());
                searchForm.setComponentName(report.getShowType());
                searchForm.setOptional(report.getOptional());
                searchForm.setQueryType(report.getQueryType());
                searchForm.setDefaultValue(report.getDefaultValue());
                searchForms.add(searchForm);
            }
        }

        Map<String,Object> map = new HashMap<>();
        map.put("searchForms",searchForms);
        map.put("tableColumns",tableColumns);
        return R.ok(map);
    }



    @PostMapping(value = "/getTableData/{formId}/{pageNum}/{pageSize}")
    public TableDataInfo getTableData(@PathVariable("formId") Long formId, @PathVariable("pageNum") Integer pageNum,
                                      @PathVariable("pageSize") Integer pageSize, @RequestBody List<Condition> conds) {
        String userName = LoginHelper.getUsername();
        Condition condition = new Condition();
        condition.setQueryField("user_name");
        condition.setQueryValue(userName);
        conds.add(condition);

        String tableName = "form_" + formId;
        List<Map<String, Object>> list = new ArrayList<>();
        int count = 0;
        if (pageSize > 0 && pageNum > 0) {
            int offset = (pageNum - 1) * pageSize;
            list = formDataService.getDataPage(tableName, "*", conds, offset, pageSize);
            count = formDataService.getDataCount(tableName, conds);
        } else {
            list = formDataService.getDataList(tableName,"*", conds);
            count = list.size();
        }
        TableDataInfo dataInfo = new TableDataInfo();
        dataInfo.setRows(list);
        dataInfo.setTotal(count);
        dataInfo.setCode(200);
        return dataInfo;
    }


    @GetMapping(value = "/getTableDataById/{formId}/{dataId}")
    public R<?> getTableDataById(@PathVariable("formId") Long formId, @PathVariable("dataId") Long dataId) {
        return R.ok(formDataService.getDataInfo("form_" + formId, dataId));
    }
}
